#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/*
 * Copyright © 2014, Finium Sdn Bhd, All Rights Reserved
 * 
 * CommonsUtil.java
 * Modification History
 * *************************************************************
 * Date				Author						Comment
 * Aug 30, 2012		Venkaiah Chowdary Koneru	Created
 * Oct 31, 2012		venkaiah Chowdary Koneru	Renamed from CoreUtil
 * *************************************************************
 */
package ${package}.support;

import ${package}.dto.FieldExtensions;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;

/**
 * Core Utility for the whole platform
 * 
 * @author Venkaiah Chowdary Koneru
 * 
 */
public final class CommonsUtil
{
    private static final Log LOG = LogFactory.getLog(CommonsUtil.class);

    /**
     * Constructor. Private to prevent unnecessary instantiation.
     */
    private CommonsUtil()
    {
    }

    /**
     * This method returns true if the collection is null or is empty.
     * 
     * @param collection
     * @return true | false
     */
    public static boolean isEmpty(Collection<?> collection)
    {
        if (collection == null || collection.isEmpty())
        {
            return true;
        }
        return false;
    }

    /**
     * This method returns true of the map is null or is empty.
     * 
     * @param map
     * @return true | false
     */
    public static boolean isEmpty(Map<?, ?> map)
    {
        if (map == null || map.isEmpty())
        {
            return true;
        }
        return false;
    }

    /**
     * This method returns true if the input array is null or its length is
     * zero.
     * 
     * @param array
     * @return true | false
     */
    public static boolean isEmpty(Object[] array)
    {
        if (array == null || array.length == 0)
        {
            return true;
        }
        return false;
    }

    /**
     * Parses the extension fields. The value will be parsed to see if it
     * represents an integer, a float, a date, or a string.
     * 
     * @param extensions
     *            an instance of {@link FieldExtensions}
     * @param value
     *            The value of the event field.
     */
    public static void parseExtensionValues(FieldExtensions extensions,
            final String value)
    {
        try
        {
            // Reset all the values
            extensions.setStrValue(null);
            extensions.setIntValue(null);
            extensions.setDoubleValue(null);
            extensions.setDateValue(null);

            extensions.setIntValue(Integer.parseInt(value));

            LOG.info("    value of extension is of type Integer.");
        }
        catch (NumberFormatException e1)
        {
            try
            {
                extensions.setDoubleValue(Double.parseDouble(value));
                LOG.info("    value of extension is of type Double.");
            }
            catch (NumberFormatException e2)
            {
                try
                {
                	// TODO: parse string date to Date object -- this depends on
                    // the individual project
                    // e.g.
                    // extensions.setDateValue(TimeParser.parseAsTimestamp(value));
                    LOG.info("    value of extension is of type Date.");
                    // TODO: remove this throwing raw exception once the above
                    // 'todo' item is fixed.
                    throw new Exception();
                }
                catch (Exception e3)
                {
                    // treat value as String
                    extensions.setStrValue(value);
                    LOG.info("    value of extension is of type String.");
                }
            }
        }
    }

    /**
     * Parses the extension fields. The value will be parsed to see if it
     * represents an integer, a float, a date, or a string.
     * 
     * @param extentionsList
     *            list of {@link FieldExtensions}
     */
    public static void parseExtensionValues(
            final List<FieldExtensions> extentionsList)
    {
        if (!CommonsUtil.isEmpty(extentionsList))
        {
            for (FieldExtensions extensions : extentionsList)
            {
                if (StringUtils.hasText(extensions.getValue()))
                {
                    parseExtensionValues(extensions, extensions.getValue());
                }
            }
        }
    }

    /**
     * compares passed in firstDate is before the passed in secondDate or not.
     * 
     * @param firstDate
     * @param secondDate
     * 
     * @return true if firstDate is before secondDate
     */
    public static boolean isBefore(Date firstDate, Date secondDate)
    {
        if (firstDate != null && secondDate != null)
        {
            return firstDate.compareTo(secondDate) < 0;
        }
        return false;
    }

    /**
     * compares passed in firstDate is after the passed in secondDate or not.
     * 
     * @param firstDate
     * @param secondDate
     * 
     * @return true if firstDate is after secondDate
     */
    public static boolean isAfter(Date firstDate, Date secondDate)
    {
        if (firstDate != null && secondDate != null)
        {
            return firstDate.compareTo(secondDate) > 0;
        }
        return false;
    }

    /**
     * compares passed in firstNumber is less than the passed in secondNumber or
     * not.
     * 
     * @param firstNumber
     * @param secondNumber
     * 
     * @return true if firstNumber is less than secondNumber
     */
    public static boolean isLessThan(Float firstNumber, Float secondNumber)
    {
        if (firstNumber != null && secondNumber != null)
        {
            return firstNumber.compareTo(secondNumber) < 0;
        }
        return false;
    }

    /**
     * compares passed in firstNumber is less than the passed in secondNumber or
     * not.
     * 
     * @param firstNumber
     * @param secondNumber
     * 
     * @return true if firstNumber is less than secondNumber
     */
    public static boolean isLessThan(Double firstNumber, Double secondNumber)
    {
        if (firstNumber != null && secondNumber != null)
        {
            return firstNumber.compareTo(secondNumber) < 0;
        }
        return false;
    }

    /**
     * compares passed in firstNumber is greater than the passed in secondNumber
     * or not.
     * 
     * @param firstNumber
     * @param secondNumber
     * 
     * @return true if firstNumber is greater than secondNumber
     */
    public static boolean isGreaterThan(Float firstNumber, Float secondNumber)
    {
        if (firstNumber != null && secondNumber != null)
        {
            return firstNumber.compareTo(secondNumber) > 0;
        }
        return false;
    }

    /**
     * compares passed in firstNumber is greater than the passed in secondNumber
     * or not.
     * 
     * @param firstNumber
     * @param secondNumber
     * 
     * @return true if firstNumber is greater than secondNumber
     */
    public static boolean isGreaterThan(Double firstNumber, Double secondNumber)
    {
        if (firstNumber != null && secondNumber != null)
        {
            return firstNumber.compareTo(secondNumber) > 0;
        }
        return false;
    }

    /**
     * compares passed in firstNumber is less than the passed in secondNumber or
     * not.
     * 
     * @param firstNumber
     * @param secondNumber
     * 
     * @return true if firstNumber is less than secondNumber
     */
    public static boolean isLessThan(Integer firstNumber, Integer secondNumber)
    {
        if (firstNumber != null && secondNumber != null)
        {
            return firstNumber.compareTo(secondNumber) < 0;
        }
        return false;
    }

    /**
     * compares passed in firstNumber is greater than the passed in secondNumber
     * or not.
     * 
     * @param firstNumber
     * @param secondNumber
     * 
     * @return true if firstNumber is greater than secondNumber
     */
    public static boolean isGreaterThan(Integer firstNumber,
            Integer secondNumber)
    {
        if (firstNumber != null && secondNumber != null)
        {
            return firstNumber.compareTo(secondNumber) > 0;
        }
        return false;
    }

    /**
     * Parses the extension fields. The values will be parsed to see if it
     * represents an integer, a float, a date, or a string.
     * 
     * @param extn
     *            FieldExtensions
     * @return String will have value and Unit of measurement(uom) by separating
     *         with white space. returns null if value and uom both are null
     */
    public static String formatFieldExtensionValue(FieldExtensions extn)
    {
        if (extn != null)
        {
            String uom = extn.getUom() == null ? "" : " " + extn.getUom();

            if (extn.getStrValue() != null)
            {
                return (extn.getStrValue() + uom);
            }
            else if (extn.getIntValue() != null)
            {
                return (Integer.toString(extn.getIntValue()) + uom);
            }
            else if (extn.getDoubleValue() != null)
            {
                return (Double.toString(extn.getDoubleValue()) + uom);
            }
            else if (extn.getDateValue() != null)
            {
                return (extn.getDateValue().toString() + uom);
            }
            else
            {
                return uom;
            }
        }
        else
        {
            return "";
        }
    }
}
